from django.shortcuts import render
from django.http.response import JsonResponse
from rest_framework.parsers import JSONParser
from rest_framework.decorators import api_view
from bug_update.models import Bug_Info
# Create your views here.

def update(request):
    try:
        data=JSONParser().parse(request)
        bug_principal=data.get("bug_principal")
        bug_title=data.get("bug_title")
        user_id=data.get("user_id")
        bug_status=data.get("bug_status")
        if user_id ==1:
            print("测试人员")
        if user_id==2:
            print("开发人员")
        if bug_title=='':
            return JsonResponse({"status": -1001, "message": "标题字段不能为空"})
        if user_id=='':
            return JsonResponse({"status":-1001,"message":"用户id不能为空"})
        if bug_principal == "测试人员" or bug_principal=='开发人员':
            Bug_Info.objects.update(**data)
            return JsonResponse({"status":1000,"message":"修改成功"})
        if bug_principal != "测试人员" and bug_principal!='开发人员':
            return JsonResponse({"status": -1001, "message": "用户身份无权操作"})
        if bug_status!=1:
            return JsonResponse({"status":"-1001","message":"bug未修复"})
    except Exception as f:
        return JsonResponse({"status":"-2002","message":f"{f}"})


def add(request):
    data = JSONParser().parse(request)
    try:
        Bug_Info(**data).save()
        return JsonResponse({"status":"1000","message":"添加成功"})
    except Exception as f:
        return JsonResponse({"status":"-1001","message":"该信息存在，添加失败"})



